package com.bsj.media.init.websocket;

import com.bsj.media.init.dto.AudioHandleInfo;
import com.bsj.media.tool.cmd.CmdReqString;
import com.bsj.springQueue.handleMap.KeyValueMap;
import lombok.extern.slf4j.Slf4j;
import org.yeauty.pojo.Session;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
public class MediaSocketChannel {

    private static KeyValueMap<Session, String> sessionMap = new KeyValueMap<>();

    private static Map<Session, AudioHandleInfo> audioMap = new ConcurrentHashMap<>();

    public static void push(String terminalNo, Session session) {
        sessionMap.push(session, terminalNo);
        //记录本次对讲的唯一类
        AudioHandleInfo audioHandleInfo = new AudioHandleInfo();
        audioHandleInfo.init(terminalNo, CmdReqString.CLIENT_TYPE_PC);
        audioMap.put(session, audioHandleInfo);
    }

    public static String del(Session session) {
        String result = null;
        sessionMap.delKey(session);
        AudioHandleInfo remove = audioMap.remove(session);
        if (remove != null) {
            result = remove.getTerminalNo();
            remove.stop();
        }
        return result;
    }

    public static void pushData(Session session, byte[] bytes) {
        AudioHandleInfo audioHandleInfo = audioMap.get(session);
        if (audioHandleInfo == null) {
            log.warn("找不到该设备下的实体数据");
            audioHandleInfo = new AudioHandleInfo();
            audioMap.put(session, audioHandleInfo);
        }
        audioHandleInfo.push(bytes);
    }
}
